package com.phy.core;

import java.io.IOException;
import javax.naming.NamingException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.phy.mail.servlets.Domains;

public class OutPut {
	private static Logger logger = Logger.getLogger(OutPut.class.getClass());

	public String[] DealLogic(int domainLength, AchieveSys sys, String domain,
			String BestMX) {
		String EmailSystemInfo = "";
		String EmailSystemShortName="";
		String temp = "";
		// 数组分别存储对应的返回值
		String output[] = { "", "", "" };
		String[] allMX = new String[domainLength];
		String noMX = "没有mx记录";

		// 输出所要查找的邮件系统信息
		// 依据验证结果输出不同的数据信息
		if (BestMX != "" && BestMX != noMX) {
			// 先用mx记录查找本地映射中的邮件系统缩略，根据mx记录判断邮件系统
			EmailSystemShortName = getEmailSystemXML(BestMX, "mx", "shortinfo");
			if (EmailSystemShortName == "0") {
				// 用优先级最高的mx记录连接服务器端的25端口获得返回值,根据返回值判断邮件系统
				EmailSystemInfo = sys.EmailSystemInfo(BestMX);
				EmailSystemShortName = getEmailSystemXML(EmailSystemInfo,
						"keyword", "abbr");
				logger.info("OutPut.104:有mx记录，可能是连接时间的原因，再找。。。");
			} else {
				EmailSystemInfo = "已根据mx记录确认邮件系统信息";
			}
			logger.info("OutPut.106:和域名服务器连接了。");
		}
		if (BestMX != "" && EmailSystemShortName == "0") {
			if (BestMX == noMX) {
				EmailSystemShortName = "Sorry，没有mx记录，无邮件系统相关信息";
			} else if (BestMX == "无法获取mx记录") {
				EmailSystemShortName = "无法获取mx记录";
			} else {
				EmailSystemShortName = "无具体邮箱信息";
				// 如果连优先级最高的mx记录都查不到邮件系统返回的信息，将使用优先级比较低的mx记录
				for (int i = 0; i < allMX.length; i++) {
					if (EmailSystemShortName == "无具体邮箱信息" && i % 2 != 0
							&& allMX[i] != BestMX && allMX[i] != null) {
						EmailSystemInfo = sys.EmailSystemInfo(allMX[i]);
						EmailSystemShortName = getEmailSystemXML(
								EmailSystemInfo, "keyword", "abbr");
					}
				}
				if (EmailSystemShortName == "无具体邮箱信息"
						&& BestMX.trim().toString().charAt(0) != '.') {
					// 真的查不到的话只好连110端口了。
					temp = sys.SocketValue(BestMX, 110, 3000);
					if (temp != "连接超时") {
						EmailSystemInfo = EmailSystemInfo + "/" + temp;
					}
					logger.info("OutPut.125:连110端口的字符串是:" + BestMX);
					
					EmailSystemShortName = getEmailSystemXML(EmailSystemInfo,
							"keyword", "abbr");
					
					if (EmailSystemShortName == "0") {
						EmailSystemShortName = getEmailSystemXML(BestMX, "mx",
								"shortinfo");
					}
					//使用返回值的信息综合判断
					if(EmailSystemInfo.indexOf("220 smtp ready")!=-1 && EmailSystemInfo.indexOf("+OK POP3 ready")!=-1){
						EmailSystemShortName="亿邮";
					}else if(EmailSystemInfo.indexOf("ESMTP")!=-1 || EmailSystemInfo.indexOf("ESMTP ready")!=-1 || EmailSystemInfo.indexOf("220 ESMTP")!=-1 || EmailSystemInfo.indexOf("220 smtp ready")!=-1){
						EmailSystemShortName="亿邮";
					}
					logger.info("OutPut.130连110端口的返回值是:" + temp);
					logger.info("OutPut.131:" + "返回的邮件系统缩略信息是:"
							+ EmailSystemShortName);
					logger.info("OutPut.132:" + "走到这里，我确认连了110端口。");
				}
				if (EmailSystemShortName == "0") {
					EmailSystemShortName = "无具体邮箱信息";
				}
			}
		}
		if (EmailSystemInfo.toString().trim() == "") {
			EmailSystemInfo = "返回值为空";
			if (BestMX != noMX && BestMX != "") { // 有mx记录，但查不出具体邮件系统信息
				EmailSystemInfo = "无相关邮件系统信息返回";
			} else if (BestMX == noMX) { // 没有mx记录
				EmailSystemInfo = noMX;
			}
		}
		output[0] = BestMX;
		output[1] = EmailSystemShortName;
		output[2] = EmailSystemInfo;
		return output;
	}
	
	// 从XML映射文件中获取邮件系统信息
	public String getEmailSystemXML(String param, String first, String second) {
		String Info = "";
		DealXML compare = null;
		try {
			compare = new DealXML();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			logger.error("OutPut.167:" + e.getMessage());
		} catch (SAXException e) {
			e.printStackTrace();
			logger.error("OutPut.170:" + e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("OutPut.173:" + e.getMessage());
		}
		Info = compare.ArchieveSysName(param, first, second);
		return Info;
	}

	public String getBestMX(int domainLength, AchieveSys sys, String domain) {
		String BestMX = "";
		String[] allMX = new String[domainLength];
		String mxs = "";// 输出到csv文件的所有 mx串
		String noMX = "没有mx记录";
		try {
			allMX = sys.ObtainAllMX(domain);
			BestMX = sys.ObtainSingleMX(allMX);
			for (int num = 0; num < allMX.length; num++) {
				mxs += allMX[num] + " ";
			}
			System.out.println("DomainName is:" + domain);
		} catch (NamingException e1) {
			e1.printStackTrace();
			System.out.println("有问题的domain是:" + domain);
			logger.error("OutPut.82:有问题的domain是:" + domain);
			BestMX = noMX;
			logger.error("OutPut.90:NamingException:" + e1.getMessage());
		}
		return BestMX;
	}
}
